package com.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 何志鹏
 * @version 1.0
 * @date 2021/10/28 17:08
 */
@RestController
@RequestMapping("/minio")
public class TestController
{

	@Value("${minio.bucket-name}")
	private String bucketName;

	@Autowired
	private MinioService minioService;

	/**
	 *上传文件
	 * @param file
	 * @return
	 */
	@PostMapping("/minioUpload")
	public  Results minioUpload(MultipartFile file){
		String fileName = file.getOriginalFilename();
		return minioService.minioUpload(file, fileName, bucketName);
	}

	/**
	 * 获取minio文件的下载地址
	 *
	 * @param fileName
	 * @return
	 * @throws Exception
	 */
	@GetMapping("/getShowUtrl/{fileName}")
	public  Results getShowUtrl(@PathVariable String fileName)throws Exception{
		String fileUrl = minioService.getFileUrl(bucketName, fileName);
		String showUtrl = minioService.getShowUtrl(fileName, 5, bucketName);
		Map<String,Object> map = new HashMap<>();
		map.put("fileUrl",fileUrl);
		map.put("showUtrl",showUtrl);
		return Results.success("",map);
	}
}
